<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>回车条款答案维护</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${basePath!}/genius/layui/css/layui.css" media="all">
</head>
<style>
    .ew-tree-table-group .ew-tree-table-box {
        overflow: auto;
        max-height: 905px;
    }
</style>
<body style="overflow: hidden;">

<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body">
            <div style="padding-bottom: 0;">
                <div class="layui-form layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <button class="layui-btn " data-type="add"><i
                                class="layui-icon layui-icon-add-circle "></i> 新增
                        </button>
                        <button class="layui-btn layui-btn-normal" data-type="edit"><i
                                class="layui-icon layui-icon-edit "></i> 编辑
                        </button>
                        <button class="layui-btn layui-btn-danger" data-type="delete"><i
                                class="layui-icon layui-icon-delete "></i> 删除
                        </button>
                    </div>
                </div>
            </div>
            <table id="clause-list" lay-filter="clause-list"></table>

        </div>
    </div>
</div>

<script src="${basePath!}/genius/layui/layui.js"></script>
<script>
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    function backUrl() {
        var bakUrl = parentJson.bakUrl;
        window.location.href = bakUrl;
    }

    let postUrl = function (params, url) { //调整页面及导出可以用此方法
        var form = document.createElement("form");
        form.style.display = 'none';
        form.action = url;
        form.method = "get";
        document.body.appendChild(form);

        for (var key in params) {
            var input = document.createElement("input");
            input.type = "hidden";
            input.name = key;
            input.value = params[key];
            form.appendChild(input);
        }
        form.submit();
        form.remove();
    }
    layui.config({
        base: '${basePath!}/genius/' //静态资源所在路径
        , host: '${basePath!}'//项目访问路径
    }).extend({
        setter: 'config'
    }).use(['table', 'form', 'util', 'laydate', 'setter'], () => {
        var $ = layui.$,
            form = layui.form,
            table = layui.table
            , setter = layui.setter;
        colsArr = [{
            type: 'checkbox',
            fixed: 'left'
        },
            {
                field: '',
                title: '序号',
                width: 40,
                type: 'numbers'
            },
            {
                title: '条款名称',
                align: 'center',
                templet: function (d) {
                    return d.clause.clauseName
                }
            },
            {
                title: '答案类型',
                align: 'center',
                templet: function (d) {
                    if (d.clause.answerType == 1) {
                        return '选择'
                    } else if (d.clause.answerType == 2) {
                        return '填空'
                    }
                }
            },
            {
                field: 'answerName',
                title: '答案',
                align: 'center',
            },
            {
                field: 'score',
                title: '分数',
                align: 'center'
            },
        ]
        table.render({
            elem: '#clause-list',
            limit: 15,
            limits: [15, 30, 50],
            cols: [colsArr],
            height: 'full-15',
            page: true, //开启分页
            // toolbar: 'default',
            method: 'post',
            url: setter.reqBase + '/follower/getAllAnswer',
            where: {
                type: 'back'
            },
            text: {
                none: '暂无相关数据'
            }
        })

        $('.layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        var active = {
            add: function () {
                // location.href = 'http://www.baidu.com'
                var obj = {};
                obj.bakUrl = '/unite_platform/backAnswerList';
                obj.jsType = 'add';
                postUrl(obj, '/unite_platform/backAnswerInsert');
            },
            delete: function () {
                var checkData = table.checkStatus('clause-list');
                if (checkData.data.length === 0) {
                    return layer.msg('请选择一行数据进行删除');
                }
                $.post(setter.reqBase + '/follower/deleteAnswer', {
                    //发送添加的出车条款表单数据
                    answer: checkData.data
                }, (res) => {
                    if (res.code == 0) {
                        layer.msg('删除成功', {
                            icon: 1
                        }, function () {
                            table.reload('clause-list')
                        });
                    } else {
                        layer.msg(res.msg);
                    }
                })
            },
            edit: function () {
                var checkData = table.checkStatus('clause-list');
                if (checkData.data.length > 1) {
                    return layer.msg('只能选中编辑一行');
                }
                if (checkData.data.length === 0) {
                    return layer.msg('请选择一行数据进行编辑');
                }
                // if (checkData.data[0].clause.clauseType == 2) {
                // 	return layer.msg('请选择一行数据进行编辑');
                // }
                layer.open({
                    type: 1,
                    title: '编辑菜单',
                    content: `<div class="layui-form layuiadmin-card-header-auto">
		<form class="layui-form" lay-filter="layuiadmin-form-admin" id="layuiadmin-form-admin">


			<input name="answer_number" type="hidden" id="answer_number" class="layui-input">
			<input name="clause_number" type="hidden" id="clause_number" class="layui-input">
			<div class="layui-form-item">
					<label class="layui-form-label">条款名称</label>
					<div class="layui-input-inline">
						<input type="text" name="clause_name" readonly id="clause_name" lay-verify="required"
							placeholder="请输入" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">答案</label>
					<div class="layui-input-inline">
						<input type="text" name="answer" id="answer" lay-verify="required"
							placeholder="请输入" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">答案类型</label>
					<div class="layui-input-inline">
						<select name="answer_type" lay-verify="required">
							<option value="1">选择</option>
							<option value="2">填空</option>
						</select>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">分数</label>
					<div class="layui-input-inline">
						<input type="number" name="score" id="score" lay-verify="required" placeholder="请输入"
							autocomplete="on" class="layui-input">

					</div>
				</div>
				
				<input type="button" class="layui-btn layui-hide layui-btn-fluid" lay-submit lay-filter="add-submit"
					id="add-submit" value="确认">
			</form>
		</div>`,
                    area: ['500px', '400px'],
                    btn: ['确定', '取消'],
                    success: function (editFrame, index) {
                        var iframeWindow = window['layui-layer-iframe' + index],
                            subBody = editFrame.find('iframe').contents();
                        // subBody.find("#id").val(checkData.data[0].clauseNumber)
                        // subBody.find("#format").val(checkData.data[0].format)
                        // subBody.find("#clause_name").val(checkData.data[0].clauseName)
                        form.val('layuiadmin-form-admin', {
                            'clause_name': checkData.data[0].clause.clauseName,
                            'answer': checkData.data[0].answerName,
                            'score': checkData.data[0].score,
                            'answer_number': checkData.data[0].answerNumber,
                            'clause_number': checkData.data[0].clauseNumber,
                            'answer_type': checkData.data[0].clause.answerType
                        })
                    },
                    yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index],
                            submitID = 'add-submit',
                            submit = layero.find('#' + submitID);

                        //监听提交
                        form.on('submit(' + submitID + ')', function (
                            data) {
                            $.post(setter.reqBase + '/follower/editAnswer', {
                                answer: data.field
                            }, (res) => {
                                if (res.code == 0) {
                                    layer.msg(res.msg);
                                    layer.close(index);
                                    table.reload('clause-list')
                                }
                            })
                        });
                        submit.trigger('click');
                    }
                });
            }
        }
    })
</script>
</body>
</html>
